﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfAppListBindingDemo1016;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
//  一般集合类型，应该用ObservableCollection类型，因为它实现了InotifyCollectionChanged和InotifyPropertyChange接口
        ObservableCollection<User> Users = new ObservableCollection<User>()
        {
            new User(){Id = 1,Name = "张三",Email = "11@123.com",Desc = "这是张三",UserName = "admin0"},
            new User(){Id = 2,Name = "李四",Email = "22@123.com",Desc = "这是李四",UserName = "admin1"},
            new User(){Id = 3,Name = "王五",Email = "33@123.com",Desc = "这是王五",UserName = "admin2"},
            new User(){Id = 4,Name = "钱七",Email = "44@123.com",Desc = "这是钱七",UserName = "admin3"},
            new User(){Id = 5,Name = "孙八",Email = "55@123.com",Desc = "这是孙八",UserName = "admin4"},
        };
        //  UserListView对应的是前端xaml中ListView元素的x:Name，他的一个属性ItemsSource被赋值，值是Users
       // UserListView.ItemsSource = Users;
       
       // DataGrid中的一个属性ItemsSource的值是Users
        DataGrid.ItemsSource = Users;

        //绑定了一个SelectedItem，下面就是这个Name
        TextBlock.SetBinding(TextBlock.TextProperty, new Binding("SelectedItem.Name") { Source = DataGrid});
    }
}